home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / domacnost a kancelar / joomla / Joomla_1.5.4-Stable-Full_Package.exe / plugins / authentication / openid.php < prev    next >
PHP Script  |  2008-07-06  |  5KB  |  165 lines

  1. <?php
  2. /**
  3.  * @version        $Id: openid.php 10497 2008-07-03 16:36:12Z ircmaxell $
  4.  * @package        Joomla
  5.  * @subpackage    JFramework
  6.  * @copyright    Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7.  * @license        GNU/GPL, see LICENSE.php
  8.  * Joomla! is free software. This version may have been modified pursuant
  9.  * to the GNU General Public License, and as distributed it includes or
  10.  * is derivative of works licensed under the GNU General Public License or
  11.  * other free or open source software licenses.
  12.  * See COPYRIGHT.php for copyright notices and details.
  13.  */
  14.  
  15. // Check to ensure this file is included in Joomla!
  16. defined('_JEXEC') or die( 'Restricted access' );
  17.  
  18. jimport( 'joomla.plugin.plugin' );
  19.  
  20. /**
  21.  * OpenID Authentication Plugin
  22.  *
  23.  * @author    Johan Janssens <johan.janssens@joomla.org>
  24.  * @package        Joomla
  25.  * @subpackage    openID
  26.  * @since 1.5
  27.  */
  28.  
  29. class plgAuthenticationOpenID extends JPlugin
  30. {
  31.     /**
  32.      * Constructor
  33.      *
  34.      * For php4 compatability we must not use the __constructor as a constructor for plugins
  35.      * because func_get_args ( void ) returns a copy of all passed arguments NOT references.
  36.      * This causes problems with cross-referencing necessary for the observer design pattern.
  37.      *
  38.      * @param     object $subject The object to observe
  39.      * @param     array  $config  An array that holds the plugin configuration
  40.      * @since 1.5
  41.      */
  42.     function plgAuthenticationOpenID(& $subject, $config)
  43.     {
  44.         parent::__construct($subject, $config);
  45.     }
  46.  
  47.     /**
  48.      * This method should handle any authentication and report back to the subject
  49.      *
  50.      * @access    public
  51.      * @param   array     $credentials Array holding the user credentials
  52.      * @param     array   $options     Array of extra options (return, entry_url)
  53.      * @param    object    $response    Authentication response object
  54.      * @return    boolean
  55.      * @since 1.5
  56.      */
  57.     function onAuthenticate( $credentials, $options, &$response )
  58.     {
  59.         global $mainframe;
  60.  
  61.         if ( !defined('Auth_OpenID_RAND_SOURCE') ) {
  62.             define ("Auth_OpenID_RAND_SOURCE", null);
  63.         }
  64.  
  65.         require_once(JPATH_LIBRARIES.DS.'openid'.DS.'consumer.php');
  66.         jimport('joomla.filesystem.folder');
  67.  
  68.         // Access the session data
  69.         $session =& JFactory::getSession();
  70.  
  71.         // Need to check for bcmath or gmp - if not, use the dumb mode.
  72.         // TODO: Should dump an error to debug saying we are dumb
  73.  
  74.         global $_Auth_OpenID_math_extensions;
  75.         $ext = Auth_OpenID_detectMathLibrary($_Auth_OpenID_math_extensions);
  76.         if (!isset($ext['extension']) || !isset($ext['class'])) {
  77.             define ("Auth_OpenID_NO_MATH_SUPPORT", true);
  78.         }
  79.  
  80.         // Create and/or start using the data store
  81.         $store_path = JPATH_ROOT . '/tmp/_joomla_openid_store';
  82.         if (!JFolder::exists($store_path) && !JFolder::create($store_path))
  83.         {
  84.             $response->type = JAUTHENTICATE_STATUS_FAILURE;
  85.             $response->error_message = "Could not create the FileStore directory '$store_path'. " . " Please check the effective permissions.";
  86.             return false;
  87.         }
  88.  
  89.         // Create store object
  90.         $store = new Auth_OpenID_FileStore($store_path);
  91.  
  92.         // Create a consumer object
  93.         $consumer = new Auth_OpenID_Consumer($store);
  94.  
  95.         if (!isset($_SESSION['_openid_consumer_last_token']))
  96.         {
  97.             // Begin the OpenID authentication process.
  98.             if(!$request = $consumer->begin($credentials['username']))
  99.             {
  100.                 $response->type = JAUTHENTICATE_STATUS_FAILURE;
  101.                 $response->error_message = 'Authentication error : could not connect to the openid server';
  102.                 return false;
  103.             }
  104.  
  105.             // Request simple registration information
  106.             $request->addExtensionArg('sreg', 'required' , 'email');
  107.             $request->addExtensionArg('sreg', 'optional', 'fullname, language, timezone');
  108.  
  109.             //Create the entry url
  110.             $entry_url  = isset($options['entry_url'])  ? $options['entry_url'] : JURI::base();
  111.             $entry_url  = JURI::getInstance($entry_url);
  112.  
  113.             unset($options['entry_url']); //We don't need this anymore
  114.  
  115.             //Create the url query information
  116.             $options['return'] = isset($options['return']) ? base64_encode($options['return']) : base64_encode(JURI::base());
  117.             $options[JUtility::getToken()] = 1;
  118.  
  119.             $process_url  = sprintf($entry_url->toString()."&username=%s", $credentials['username']);
  120.             $process_url .= '&'.JURI::buildQuery($options);
  121.  
  122.             $trust_url    = $entry_url->toString(array('path', 'host', 'port', 'scheme'));
  123.             $redirect_url = $request->redirectURL($trust_url, $process_url);
  124.  
  125.             $session->set('trust_url', $trust_url);
  126.  
  127.             // Redirect the user to the OpenID server for authentication.  Store
  128.             // the token for this authentication so we can verify the response.
  129.             $mainframe->redirect($redirect_url);
  130.  
  131.             return false;
  132.         }
  133.  
  134.         $result = $consumer->complete(JRequest::get('get'));
  135.  
  136.         switch ($result->status)
  137.         {
  138.             case Auth_OpenID_SUCCESS :
  139.             {
  140.                 $sreg = $result->extensionResponse('sreg');
  141.  
  142.                 $response->status          = JAUTHENTICATE_STATUS_SUCCESS;
  143.                 $response->error_message  = '';
  144.                 $response->email    = isset($sreg['email'])    ? $sreg['email']    : "";
  145.                 $response->fullname    = isset($sreg['fullname']) ? $sreg['fullname'] : "";
  146.                 $response->language    = isset($sreg['language']) ? $sreg['language'] : "";
  147.                 $response->timezone    = isset($sreg['timezone']) ? $sreg['timezone'] : "";
  148.  
  149.             } break;
  150.  
  151.             case Auth_OpenID_CANCEL :
  152.             {
  153.                 $response->status = JAUTHENTICATE_STATUS_CANCEL;
  154.                 $response->error_message = 'Authentication cancelled';
  155.             } break;
  156.  
  157.             case Auth_OpenID_FAILURE :
  158.             {
  159.                 $response->status = JAUTHENTICATE_STATUS_FAILURE;
  160.                 $response->error_message = 'Authentication failed';
  161.             } break;
  162.         }
  163.     }
  164. }
  165.